import axios from 'axios'
import store from '../store/state'
import router from '../router'
import {ElMessage} from 'element-plus';

// 通过proxy的方式解决跨域
// build时，通过nginx转发，不需要指定后端地址




// axios.defaults.withCredentials = true;
// axios.defaults.baseURL = baseUrl;
// axios.defaults.baseURL = '/api'
axios.defaults.baseURL = 'http://localhost:8500'
axios.defaults.headers['Content-Type']='application/json'
const err_msg_duration = 2000 // ms


axios.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    // 判断是否存在token,如果存在将每个页面header添加token
    let token=sessionStorage.getItem("token")
    if (!config.url.startsWith("/api/user/") || config.url === "/api/user/list/") 
    {
        config.headers.Authorization = "Token"+'\t'+token;
    }
    return config
  }, function (error) 
{
    return Promise.reject(error);
});

axios.interceptors.response.use(function (response) {

    return response;
}, function (error) {
    try {

        if (error.response.status === 401) {
            router.replace({
                name: 'Login'
            })
        }
        if (error.response.status === 500) {
            ElMessage.error({
                message: `哦豁,服务器出现了一点状况.trace_id:${error.response.headers['trace-id']}`,
                duration: err_msg_duration
            })
        }
        if (error.response.status === 403) {
            ElMessage.error({
                message: `哦豁，权限方面似乎出了点问题, ${error.response.data.msg}`,
                duration: err_msg_duration
            })
        }
    } catch (e) {
        ElMessage.error({
            message: '服务器连接超时，请重试',
            duration: err_msg_duration
        })
    }
});

// user api
export const register = params => {
    return axios.post('/api/user/register/', params).then(res => res.data)
};

// export const login = params => {
//     return axios.post('/api/user/login/', params).then(res => res.data)
// };
export const login = params => {

    return axios.post('/api/user/login/', params)
        .then(res => {
            // 检查 'data' 字段是否存在
            if (res && res.data) {
                return res.data;
            } else {
                // 如果 'data' 字段不存在，可能是服务器返回了一个不符合预期的响应
                throw new Error('Unexpected response format');
            }
        })
        .catch(error => {
            // 处理请求失败或网络错误
            console.error('Login request error:', error.message);
            throw error;  // 将错误传递给调用者
        });
};
export const getUserList = () => {
    return axios.get('/api/user/list/').then(res => res.data)
};


// aweirunner api
export const addProject = params => {
    return axios.post('/api/aweirunner/project/', params).then(res => res.data)
};

export const deleteProject = config => {
    return axios.delete('/api/aweirunner/project/', config).then(res => res.data)
};

export const getProjectList = params => {
    return axios.get('/api/aweirunner/project/').then(res => res.data)
};

export const getProjectDetail = pk => {
    return axios.get('/api/aweirunner/project/' + pk + '/').then(res => res.data)
};

export const getDashBoard = () => {
    return axios.get('/api/aweirunner/dashboard/').then(res => res.data)
};

export const getProjectYapiInfo = pk => {
    return axios.get('/api/aweirunner/project/yapi/' + pk + '/').then(res => res.data)
};

export const getVisit = params => {
    return axios.get('/api/aweirunner/visit/', params).then(res => res.data)
};

export const getPagination = url => {
    return axios.get(url).then(res => res.data)
};

export const updateProject = params => {
    return axios.patch('/api/aweirunner/project/', params).then(res => res.data)
};

export const getDebugtalk = url => {
    return axios.get('/api/aweirunner/debugtalk/' + url + '/').then(res => res.data)
};

export const updateDebugtalk = params => {
    return axios.patch('/api/aweirunner/debugtalk/', params).then(res => res.data)
};

export const runDebugtalk = params => {
    return axios.post('/api/aweirunner/debugtalk/', params).then(res => res.data)
};

export const getTree = (url, params) => {
    return axios.get('/api/aweirunner/tree/' + url + '/', params).then(res => res.data.data)
};

export const updateTree = (url, params) => {
    return axios.patch('/api/aweirunner/tree/' + url + '/', params).then(res => res.data)
};

export const uploadFile = url => {
    return '/api/aweirunner/file/?token=' + store.token
};

export const addAPI = params => {
    return axios.post('/api/aweirunner/api/', params).then(res => res.data)
};

export const updateAPI = (url, params) => {
    return axios.patch('/api/aweirunner/api/' + url + '/', params).then(res => res.data)
};


export const apiList = params => {
    return axios.get('/api/aweirunner/api/', params).then(res => res.data)
};

export const delAPI = url => {
    return axios.delete('/api/aweirunner/api/' + url + '/').then(res => res.data)
};

export const tagAPI = params => {
    return axios.patch('/api/aweirunner/api/tag/', params).then(res => res.data)
};

export const syncCaseStep = (url, params) => {
    return axios.patch('/api/aweirunner/api/sync/' + url + '/').then(res => res.data)
};

export const delAllAPI = params => {
    return axios.delete('/api/aweirunner/api/', params).then(res => res.data)
};

export const getAPISingle = url => {
    return axios.get('/api/aweirunner/api/' + url + '/').then(res => res.data)
};


export const getPaginationBypage = params => {
    return axios.get('/api/aweirunner/api/', params).then(res => res.data)
};


export const addTestCase = params => {
    return axios.post('/api/aweirunner/test/', params).then(res => res.data)
};

export const updateTestCase = (url, params) => {
    return axios.patch('/api/aweirunner/test/' + url + '/', params).then(res => res.data)
};

export const testList = params => {
    return axios.get('/api/aweirunner/test/', params).then(res => res.data)
};

export const deleteTest = url => {
    return axios.delete('/api/aweirunner/test/' + url + '/').then(res => res.data)
};

export const syncTest = url => {
    return axios.put('/api/aweirunner/test/' + url + '/').then(res => res.data)
};
export const delAllTest = params => {
    return axios.delete('/api/aweirunner/test/', params).then(res => res.data)
};

export const coptTest = (url, params) => {
    return axios.post('/api/aweirunner/test/' + url + '/', params).then(res => res.data)
};

export const editTest = url => {
    return axios.get('/api/aweirunner/teststep/' + url + '/').then(res => res.data)
};

export const getTestPaginationBypage = params => {
    return axios.get('/api/aweirunner/test/', params).then(res => res.data)
};

export const addConfig = params => {
    return axios.post('/api/aweirunner/config/', params).then(res => res.data)
};

export const updateConfig = (url, params) => {
    return axios.patch('/api/aweirunner/config/' + url + '/', params).then(res => res.data)
};


export const configList = params => {
    return axios.get('/api/aweirunner/config/', params).then(res => res.data)
};

export const copyConfig = (url, params) => {
    return axios.post('/api/aweirunner/config/' + url + '/', params).then(res => res.data)
};

export const copyAPI = (url, params) => {
    return axios.post('/api/aweirunner/api/' + url + '/', params).then(res => res.data)
};

export const deleteConfig = url => {
    return axios.delete('/api/aweirunner/config/' + url + '/').then(res => res.data)
};
export const delAllConfig = params => {
    return axios.delete('/api/aweirunner/config/', params).then(res => res.data)
};

export const getConfigPaginationBypage = params => {
    return axios.get('/api/aweirunner/config/', params).then(res => res.data)
};

export const getAllConfig = url => {
    return axios.get('/api/aweirunner/config/' + url + '/').then(res => res.data)
};


export const runSingleAPI = params => {
    return axios.post('/api/aweirunner/run_api/', params).then(res => res.data)
};

export const runAPIByPk = (url, params) => {
    return axios.get('/api/aweirunner/run_api_pk/' + url + '/', params).then(res => res.data)
};

export const runAPITree = params => {
    return axios.post('/api/aweirunner/run_api_tree/', params).then(res => res.data)
};

export const moveAPI = params => {
    return axios.patch('/api/aweirunner/api/move_api/', params).then(res => res.data)
};

export const moveCase = params => {
    return axios.patch('/api/aweirunner/test/move_case/', params).then(res => res.data)
};

export const tagCase = params => {
    return axios.patch('/api/aweirunner/test/tag/', params).then(res => res.data)
};

export const runSingleTestSuite = params => {
    return axios.post('/api/aweirunner/run_testsuite/', params).then(res => res.data)
};

export const runSingleTest = params => {
    return axios.post('/api/aweirunner/run_test/', params).then(res => res.data)
};

export const runMultiTest = params => {
    return axios.post('/api/aweirunner/run_multi_tests/', params).then(res => res.data)
};

export const runTestByPk = (url, params) => {
    return axios.get('/api/aweirunner/run_testsuite_pk/' + url + '/', params).then(res => res.data)
};

export const runSuiteTree = params => {
    return axios.post('/api/aweirunner/run_suite_tree/', params).then(res => res.data)
};

export const addVariables = params => {
    return axios.post('/api/aweirunner/variables/', params).then(res => res.data)
};

export const variablesList = params => {
    return axios.get('/api/aweirunner/variables/', params).then(res => res.data)
};

export const getVariablesPaginationBypage = params => {
    return axios.get('/api/aweirunner/variables/', params).then(res => res.data)
};


export const updateVariables = (url, params) => {
    return axios.patch('/api/aweirunner/variables/' + url + '/', params).then(res => res.data)
};

export const updateTask = (url, params, data) => {
    return axios({
        url: '/api/aweirunner/schedule/' + url + '/',
        method: 'PUT',
        params: params,
        data: data
    }).then(res => res.data)
};

export const patchTask = (url, params) => {
    return axios.patch('/api/aweirunner/schedule/' + url + '/', params).then(res => res.data)
};

export const deleteVariables = url => {
    return axios.delete('/api/aweirunner/variables/' + url + '/').then(res => res.data)
};

export const delAllVariabels = params => {
    return axios.delete('/api/aweirunner/variables/', params).then(res => res.data)
};

export const reportList = params => {
    return axios.get('/api/aweirunner/report/', params).then(res => res.data)
};

export const deleteReports = url => {
    return axios.delete('/api/aweirunner/report/' + url + '/').then(res => res.data)
};

export const getReportsPaginationBypage = params => {
    return axios.get('/api/aweirunner/report/', params).then(res => res.data)
};

export const delAllReports = params => {
    return axios.delete('/api/aweirunner/report/', params).then(res => res.data)
};

export const watchSingleReports = url => {
    return axios.get('/api/aweirunner/report/' + url + '/').then(res => res.data)
};

export const addTask = params => {
    return axios.post('/api/aweirunner/schedule/', params).then(res => res.data)
};

export const copyTask = (task_id, params) => {
    return axios.post('/api/aweirunner/schedule/' + task_id + '/', params).then(res => res.data)
};
export const taskList = params => {
    return axios.get('/api/aweirunner/schedule/', params).then(res => res.data)
};
export const getTaskPaginationBypage = params => {
    return axios.get('/api/aweirunner/schedule/', params).then(res => res.data)
};
export const deleteTasks = url => {
    return axios.delete('/api/aweirunner/schedule/' + url + '/').then(res => res.data)
};

export const runTask = url => {
    return axios.get('/api/aweirunner/schedule/' + url + '/').then(res => res.data)
};

export const addHostIP = params => {
    return axios.post('/api/aweirunner/host_ip/', params).then(res => res.data)
};

export const hostList = params => {
    return axios.get('/api/aweirunner/host_ip/', params).then(res => res.data)
};

export const updateHost = (url, params) => {
    return axios.patch('/api/aweirunner/host_ip/' + url + '/', params).then(res => res.data)
};

export const deleteHost = url => {
    return axios.delete('/api/aweirunner/host_ip/' + url + '/').then(res => res.data)
};
export const getReport = (url, params) => {
    return axios.get('/api/aweirunner/report/'+ url + '/',params).then(res => res.data)
};

// export const getHostPaginationBypage = params => {
//     return axios.get('/api/aweirunner/host_ip/', params).then(res => res.data)
// };

// export const getAllHost = url => {
//     return axios.get('/api/aweirunner/host_ip/' + url + '/').then(res => res.data)
// };

